'use strict';
const request = require('request');
const fs = require('fs');
//花瓣网美女频道
//Host: huaban.com
//Referer: http://huaban.com/explore/meijia/
//Accept: application/json
//Cookie: referer=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%25E8%258A%25B1%25E7%2593%25A3%25E7%25BD%2591%26rsv_spt%3D1%26issp%3D1%26f%3D3%26rsv_bp%3D0%26rsv_idx%3D2%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26rsv_enter%3D1%26rsv_sug3%3D6%26rsv_sug1%3D6%26oq%3D%25E8%258A%25B1%25E7%2593%25A3%26rsv_sug2%3D1%26rsp%3D0%26inputT%3D3504%26rsv_sug4%3D4163; _dc=1; sid=H2DiqEkwhO6AsV5lxav9A9Kn.6FAU5fLVbng16OOOpYmxft2GmAL2Mxm8HQ7a%2F0d7QoQ; _ga=GA1.2.132899006.1423118375; __asc=8da119c514bddac4c12179d1a8d; __auc=8da119c514bddac4c12179d1a8d
//Accept-Encoding: gzip, deflate, sdch
//Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
//Connection: keep-alive
//Host: huaban.com
function getImageUrls(url) {
    return new Promise((resolve, reject) => {
        request({
            url: url,
            headers: {
                'X-Request': 'JSON',
                'X-Requested-With': 'XMLHttpRequest'
            }
        }, function(err, response, body) {
            if (err) return reject(err);
            if (response.statusCode != 200) return reject('get ajax json fail: ' + response.statusCode);
            let data = JSON.parse(body);
            let urls = data.pins.map((pin) => {
                return 'http://hbimg.b0.upaiyun.com/' + pin.file.key;
            });
            resolve({
                urls: urls,
                starid: data.pins[data.pins.length - 1].pin_id
            });
        })
    });
}

function downloadImage(urls) {
    fs.writeFile('./hello.json',urls,(err)=>console.log(err));
    // urls.forEach((url) => {
    //     let savePath = './download/' + url.substr(url.lastIndexOf('/')+1) + '.jpg';
    //     request.get(url).on('error', function(err) {
    //         console.log(err)
    //     }).pipe(fs.createWriteStream(savePath));
    // });
}

function compositeUrl(startid) {
    return 'http://huaban.com/favorite/beauty/?isczivzd&max=' + startid + '&limit=10&wfl=1'
}


function nice(startid,limit){
	if(limit <= 0) return;
	request({
            url: compositeUrl(startid),
            headers: {
                'X-Request': 'JSON',
                'X-Requested-With': 'XMLHttpRequest'
            }
        }, function(err, response, body) {
            if (err) return reject(err);
            if (response.statusCode != 200) return reject('get ajax json fail: ' + response.statusCode);
            let data = JSON.parse(body);
            let urls = data.pins.map((pin) => {
                return 'http://hbimg.b0.upaiyun.com/' + pin.file.key;
            });
            downloadImage(urls);
           	limit--;
           	nice(data.pins[data.pins.length - 1].pin_id,limit);
        })
}
nice(834456365,100);


//1. 爬50~100条记录，随机10秒内再爬50~100条,存入数据库
//2. 读取数据库，获取未下载&&!正在下载的url，下载图片

